package team.travel.servlet;


	import java.io.IOException;
	import javax.servlet.ServletException;
	import javax.servlet.http.HttpServlet;
	import javax.servlet.http.HttpServletRequest;
	import javax.servlet.http.HttpServletResponse;

	import team.travel.User;
	import team.travel.dao.UserDao;
import team.travel.inforsecurity.EncryptionUtil;
	/**
	 * 用户登录Servlet类
	 * @author KangJingbo
	 */
	public class LoginServlet extends HttpServlet {
		private static final long serialVersionUID = -3009431503363456775L;
		
		public void doPost(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			// 获取用户名
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");
			String username = request.getParameter("username");
			int id = Integer.parseInt(username);
			// 获取密码
			String password = EncryptionUtil.MD5(request.getParameter("password"));
			
			// 实例化UserDao对象
			UserDao userDao = new UserDao();
			// 根据用户密码查询用户
			User user = userDao.login(id, password);
			// 判断user是否为空
			if(user != null){
				// 将用户对象放入session中
				request.getSession().setAttribute("user", user);
				// 转发到result.jsp页面
				request.getRequestDispatcher("index.jsp").forward(request, response);
			}else{
				// 登录失败
				request.setAttribute("info", "错误：用户名或密码错误！");
				request.getRequestDispatcher("webpage/Login.jsp").forward(request, response);
			}
		}

	}


